//
//  LXTableViewCell.m
//  tableViewOptimize
//
//  Created by imera on 16/5/5.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXTableViewCell.h"
#import "UIImageView+WebCache.h"

#define LXColor(r, g, b) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1]

@interface LXTableViewCell()

@property (nonatomic, weak) UIImageView *mainView;

@property (nonatomic, weak) UIImageView *userPhoto;

@property (nonatomic, weak) UIScrollView *thumbPictures;

@end

@implementation LXTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIImageView *mainView = [[UIImageView alloc]init];
        [self addSubview:mainView];
        self.mainView = mainView;
        
        /** 用户头像 */
        UIImageView *userPhoto = [[UIImageView alloc]init];
        self.userPhoto = userPhoto;
        [self addSubview:userPhoto];
        
        UIScrollView *thumbPictures = [[UIScrollView alloc]init];
        thumbPictures.hidden = YES;
        self.thumbPictures = thumbPictures;
        [self addSubview:thumbPictures];
    }
    return self;
}

/** 绘制图像到cell上 */
- (void)draw:(NSDictionary *)data
{  
//    NSLog(@"%@", data);
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        CGRect rect = [data[@"frame"] CGRectValue];
        UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1] set];
        CGContextFillRect(context, rect);
        
        NSString *name = data[@"name"];
        UIFont *nameFont = [UIFont systemFontOfSize:SIZE_FONT_NAME];
        CGSize nameSize = [name sizeWithAttributes:@{NSFontAttributeName:nameFont}];
        CGFloat nameX = SIZE_AVATAR + SIZE_GAP_LEFT * 2;
        [name drawInRect:CGRectMake(nameX, SIZE_GAP_TOP, nameSize.width, nameSize.height) withAttributes:@{NSFontAttributeName:nameFont, NSForegroundColorAttributeName:[UIColor colorWithRed:106/255.0 green:140/255.0 blue:181/255.0 alpha:1]}];
        
        UIFont *subTitleFont = [UIFont systemFontOfSize:SIZE_FONT_SUBTITLE];
        NSString *time = data[@"time"];
        CGSize timeSize = [time sizeWithAttributes:@{NSFontAttributeName:subTitleFont}];
        CGFloat timeY = SIZE_GAP_TOP + nameSize.height;
        [time drawInRect:CGRectMake(nameX, timeY , timeSize.width, timeSize.height) withAttributes:@{NSFontAttributeName:subTitleFont}];
        
        NSString *from = data[@"from"];
        CGSize fromSize = [from sizeWithAttributes:@{NSFontAttributeName:subTitleFont}];
        CGFloat fromX = nameX + timeSize.width + SIZE_GAP_LEFT;
        CGFloat fromY = SIZE_GAP_TOP + nameSize.height;
        [from drawInRect:CGRectMake(fromX, fromY, fromSize.width, fromSize.height) withAttributes:@{NSFontAttributeName:subTitleFont}];
        
        NSString *text = data[@"text"];
        UIFont *textFont = [UIFont systemFontOfSize:SIZE_FONT_CONTENT];
        CGSize textSize = [text rectWithFont:textFont maxWidth:[UIScreen screenWidth] - SIZE_GAP_LEFT * 2];
        CGFloat textY = fromY + fromSize.height + SIZE_GAP_TOP;
        [text drawInRect:CGRectMake(SIZE_GAP_LEFT, textY, textSize.width, textSize.height) withAttributes:@{NSFontAttributeName:textFont, NSForegroundColorAttributeName:[UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1]}];
    
        if ([data valueForKey:@"pic_urls"]) {
            UIScrollView *thumbPictures = self.thumbPictures;
            CGFloat thumbPicturesY = textY + textSize.height + SIZE_GAP_TOP;
            thumbPictures.frame = CGRectMake(SIZE_GAP_LEFT, thumbPicturesY, [UIScreen screenWidth], 70);
        }
    
        if([data valueForKey:@"subData"]){
            [[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1] set];
            CGRect rect = [data[@"subData"][@"frame"] CGRectValue];
            CGContextFillRect(context, rect);
            
            NSDictionary *subData = data[@"subData"];
            NSString *subConetnt = [NSString stringWithFormat:@"%@", subData[@"text"]];
            CGFloat maxWidth = [UIScreen screenWidth] - SIZE_GAP_LEFT * 2;
            CGFloat subConetntY = rect.origin.y + SIZE_GAP_TOP;
            CGSize subDataSize = [subConetnt rectWithFont:[UIFont systemFontOfSize:SIZE_FONT_SUBCONTENT] maxWidth:maxWidth];
            [subConetnt drawInRect:CGRectMake(SIZE_GAP_LEFT, subConetntY, subDataSize.width, subDataSize.height) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SIZE_FONT_SUBCONTENT]}];
            
            NSArray *thumbPicturesArr = subData[@"pic_urls"];
            if (thumbPicturesArr != nil) {
                UIScrollView *thumbPictures = self.thumbPictures;
                CGFloat thumbPicturesY = subConetntY + subDataSize.height + SIZE_GAP_TOP;
                thumbPictures.frame = CGRectMake(SIZE_GAP_LEFT, thumbPicturesY, maxWidth, 70);
            }
        }
    
        if (self.contentView != nil) {
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            self.mainView.frame = rect;
            self.mainView.image = image;
        }
        
        
        UIGraphicsEndImageContext();
    
    [self loadThumbPictures:data];
//    });
}

/** 加载缓存图片 */
- (void)loadThumbPictures:(NSDictionary *)data
{
    UIImageView *userPhoto = self.userPhoto;
    [userPhoto sd_setImageWithURL:[NSURL URLWithString:data[@"avatarUrl"]]];
    
    CGFloat userPhotoX = SIZE_GAP_LEFT;
    CGFloat userPhotoY = SIZE_GAP_TOP;
    CGFloat userPhotoW = SIZE_AVATAR;
    CGFloat userPhotoH = userPhotoW;
    userPhoto.frame = CGRectMake(userPhotoX, userPhotoY, userPhotoW, userPhotoH);
    
    userPhoto.layer.cornerRadius = userPhotoW * 0.5;     //设置圆角
    userPhoto.layer.masksToBounds = YES;
    
    UIScrollView *thumbPictures = self.thumbPictures;
    [self removeSubViews:thumbPictures];
    
    NSArray *thumbArr = [data valueForKey:@"pic_urls"];
    if (thumbArr.count != 0) {  //有图片
        thumbPictures.hidden = NO;
        [self addThumbPictures:thumbArr];
    }else if ([data valueForKey:@"subData"]) {
        NSDictionary *subData = data[@"subData"];
        NSArray *thumbPicturesArr = subData[@"pic_urls"];
        if (thumbPicturesArr.count > 0) { //有转发图
            thumbPictures.hidden = NO;
            [self addThumbPictures:thumbPicturesArr];
        }else {
            thumbPictures.hidden = YES;
        }
    }else{ 
        thumbPictures.hidden = YES;
    }
    
}

/** 添加缩略图 */
- (void)addThumbPictures:(NSArray *)arr
{
    CGFloat imageWH = 70;
    for (int i=0;i<arr.count;i++) {
        NSDictionary *dict = arr[i];
        UIImageView *imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:dict[@"thumbnail_pic"]]];
        [self.thumbPictures addSubview:imageView];
        imageView.frame = CGRectMake(i * imageWH, 0, imageWH, imageWH);
    }
    self.thumbPictures.contentSize = CGSizeMake(arr.count * imageWH, imageWH);
}

/** 移除子控件 */
- (void)removeSubViews:(UIView *)view
{
    NSArray *arr = view.subviews;
    for (UIView *subView in arr) {
        [subView removeFromSuperview];
    }
}

/** 清除cell内容 */
- (void)clear
{
    self.userPhoto.image = nil;
    self.mainView.image = nil;
    self.thumbPictures.backgroundColor = [UIColor clearColor];
    self.thumbPictures.frame = CGRectZero;
}
@end
